package com.wjk.cms.service.impl;

import com.wjk.cms.dao.CardRepository;
import com.wjk.cms.pojo.Card;
import com.wjk.cms.service.CardService;
import com.wjk.cms.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author WUGN
 * @des
 * @date 2021/10/12 9:15
 */
@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardRepository cardRepository;


    @Override
    public List<Card> list(Long userId, String keywords,String status, String categoryId,String flag,String startTime,String endTime,Integer pageNo, Integer pageSize) {
        if(pageNo==null || pageNo<1){
            return cardRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(keywords)) {
                    predicates.add(cb.like(root.get("code"),"%"+keywords+"%"));

                }
                if (userId!=null && userId>0) {
                    predicates.add(cb.equal(root.get("userId"),userId));

                }
                if (StringUtils.isNotBlank(status)) {
                    predicates.add(cb.equal(root.get("status"),status));

                }
                if (StringUtils.isNotBlank(flag)) {//激活时间
                    Date now = new Date();
                    if(flag.equals("0")){//近一周
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-7, Calendar.DATE)));
                    }else if(flag.equals("1")){//近一月
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-1, Calendar.MONTH)));
                    }else if(flag.equals("2")){//近三月
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-3, Calendar.MONTH)));
                    }else if(flag.equals("3")){//近半年
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-6, Calendar.MONTH)));
                    }

                }

                if (StringUtils.isNotBlank(categoryId)) {
                    predicates.add(cb.equal(root.get("categoryId"),categoryId));

                }


                return cb.and(predicates.toArray(new Predicate[0]));
            }, Sort.by(Sort.Order.desc("createTime")));
        }else {
            return cardRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(keywords)) {
                    predicates.add(cb.like(root.get("code"),"%"+keywords+"%"));

                }
                if (userId!=null && userId>0) {
                    predicates.add(cb.equal(root.get("userId"),userId));

                }
                if (StringUtils.isNotBlank(status)) {
                    predicates.add(cb.equal(root.get("status"),status));

                }

                if (StringUtils.isNotBlank(categoryId)) {
                    predicates.add(cb.equal(root.get("categoryId"),categoryId));

                }

                if (StringUtils.isNotBlank(flag)) {//激活时间
                    Date now = new Date();
                    if(flag.equals("0")){//近一周
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-7, Calendar.DATE)));
                    }else if(flag.equals("1")){//近一月
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-1, Calendar.MONTH)));
                    }else if(flag.equals("2")){//近三月
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-3, Calendar.MONTH)));
                    }else if(flag.equals("3")){//近半年
                        predicates.add(cb.greaterThanOrEqualTo(root.get("activeTime"), DateUtil.getNewDate(now,-6, Calendar.MONTH)));
                    }

                }

                return cb.and(predicates.toArray(new Predicate[0]));
            }, PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime")).getContent();
        }
    }

    @Override
    public Card getById(Long cardId) {
        return cardRepository.getOne(cardId);
    }

    @Override
    public void save(Card card) {
        cardRepository.save(card);
    }

    @Override
    public Card getByCode(String code) {
        return cardRepository.getByCode(code);
    }
}
